package com.woniuxy.yoga.business.web.controller.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.woniuxy.yoga.business.model.exception.BusinessException;
import com.woniuxy.yoga.business.model.exception.BusinessExceptionCode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class xxlUtil {

    /**
     * 删除定时任务
     * @param id 任务ID
     */
    public static void removeTack(Long id){
        //登录
        String xxlLoginAPI="http://192.168.90.249:9999/xxl-job-admin/login";
        HashMap<String, Object> loginParam = new HashMap<>();
        loginParam.put("userName", "admin");
        loginParam.put("password", "123456");
        HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
        if (httpResponse.getStatus()==200){
            String xxlRemoveJobAPI = "http://192.168.90.249:9999/xxl-job-admin/jobinfo/remove";
            HashMap<String, Object> param = new HashMap<>();
            param.put("id",id);
            HttpResponse response = HttpRequest.post(xxlRemoveJobAPI).form(param).execute();
            System.out.println("response.body() = " + response.body());
            if (response.getStatus()==200){
                System.out.println(id+"号任务，删除成功！");
            }else{
                System.out.println(id+"号任务，删除失败！");
            }
        }else{
            System.out.println("xxl登录失败");
        }
    }

    /**
     * 添加定时任务
     * @param
     * @param tackName 任务名称
     * @param author 作者
     * @param JobHeader XxlJob处理路径
     * @param
     * @return 响应体body的JOSN字符串
     */
    public static String addTackTow(String tackName, String author, String JobHeader){
        //登录
        String xxlLoginAPI="http://192.168.90.249:9999/xxl-job-admin/login";
        HashMap<String, Object> loginParam = new HashMap<>();
        loginParam.put("userName", "admin");
        loginParam.put("password", "123456");
        HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
        String body = null;
        //登陆成功
        if(httpResponse.getStatus()==200) {
            System.out.println("xxl登录成功");
            Map<String, Object> addTaskParam = new HashMap<>();
            StringBuilder cron = new StringBuilder();
            cron.append("0 0 1 1 * ?");
            //设置请求参数
            addTaskParam.put("jobGroup", 8);//执行器
            addTaskParam.put("jobDesc", tackName);//任务名称
            addTaskParam.put("author", author);//作者
            addTaskParam.put("alarmEmail","@xxx.com");//邮箱
            addTaskParam.put("scheduleType", "CRON");//调度类型
            addTaskParam.put("scheduleConf", cron.toString());//Cron表达式
            addTaskParam.put("glueType", "BEAN");//运行模式
            addTaskParam.put("executorHandler", JobHeader);//JobHandler
            addTaskParam.put("executorRouteStrategy", "FIRST");//路由策略
            addTaskParam.put("misfireStrategy","DO_NOTHING");//调度过期策略
            addTaskParam.put("executorBlockStrategy", "SERIAL_EXECUTION");//阻塞处理策略
            addTaskParam.put("executorTimeout", "0");//任务超时时间
            addTaskParam.put("executorFailRetryCount","0");//失败重试次数
            addTaskParam.put("triggerStatus", 1);
            //查询该定时任务是否已存在
            HashMap<String, Object> param = new HashMap<>();
            param.put("jobGroup",8);
            param.put("jobDesc","每月1号定时统计月营业额");
            param.put("triggerStatus",1);
            String xxlQueryJobAPI="http://192.168.90.249:9999/xxl-job-admin/jobinfo/pageList";
            HttpResponse one = HttpRequest.get(xxlQueryJobAPI).form(param).execute();
            List data = (List) JSONUtil.parseObj(one.body()).get("data");
            if (data!=null&&data.size()>0) {
                throw new BusinessException(BusinessExceptionCode.BUSINESS_EXCEPTION_CODE_COUNT_HASTASK);
            }
            //发送请求
            String xxlAddJobAPI="http://192.168.90.249:9999/xxl-job-admin/jobinfo/add";
            HttpResponse response = HttpRequest.post(xxlAddJobAPI).form(addTaskParam).execute();
            System.out.println(response.body());
            System.out.println("response.getStatus() = " + response.getStatus());
            if(response.getStatus()!=200) {
                return null;
            }else{
                System.out.println("定时任务设置成功");
            }
            body = response.body();
        }else{
            System.out.println("xxl登录失败");
        }
        return body;
    }
}
